using Digitalmes.Domain.Entities.Mes.Processes;

namespace Digitalmes.Application.Mes.Processes.RoutingSteps.Queries;

/// <summary>
/// 工艺步骤查询命令
/// </summary>
public sealed record GetRoutingStepListQuery(long RoutingId) : IRequest<IResult<List<ProcRoutingStepDto>>>;

internal sealed class GetRoutingStepListQueryHandler(IRepository<ProcRoutingStep> repo) : IRequestHandler<GetRoutingStepListQuery, IResult<List<ProcRoutingStepDto>>>
{
    public async Task<IResult<List<ProcRoutingStepDto>>> Handle(GetRoutingStepListQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .Includes(m => m.Operation)
            .Where(m => m.RoutingId == request.RoutingId)
            .OrderBy(m => m.Seq)
            .ToListAsync(cancellationToken);

        return Result.From(items.Adapt<List<ProcRoutingStepDto>>());
    }
}
